#ifndef MAPCALIBRATOR_H
#define MAPCALIBRATOR_H

#include "controls/rotatingwidget.h"
#include <QtGui>

#include "map.h"

class FTPushButton;
class FTPushButtonIcon;
class FTLabel;
class FTLatLonEdit;
class FTHelpWidget;

class MapCalibrator : public RotatingWidget
{
    Q_OBJECT
public:
    explicit MapCalibrator(QWidget *parent = 0);

    void paintEvent(QPaintEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);

signals:
    void mapCalibrated(QList<map_cal_point>);
    void abortCalibration();

public slots:
    void setupCalibrator( const Map*, const QList<map_cal_point>& );

private slots:
    void saveCalibration();
    void zoomIn();
    void zoomOut();
    void addPoint();
    void savePoint();
    void deletePoint();
    void fromGps();
    void openEditPoint();

private:
    enum pages { main_page, point_page };
    enum state { rest, move_screen, drag_point };

private:
    void buildMainPage();
    void buildPointPage();

    void setCurrentPage( MapCalibrator::pages );

    pages currentPage;
    QStackedWidget* pagesWidget;

    // main page
    QWidget* mainPage;
    FTPushButton* cancelButton;
    FTPushButton* saveButton;
    FTPushButtonIcon* zoomInButton;
    FTPushButtonIcon* zoomOutButton;
    FTPushButtonIcon* addPointButton;
    FTHelpWidget* help1;

    // points page
    QWidget* pointPage;
    FTLabel* pointTitle;
    FTLabel* xLabel;
    FTLabel* yLabel;
    FTLabel* latLonLabel;
    FTLatLonEdit* latLonEdit;
    QLineEdit* xEdit;
    QLineEdit* yEdit;
    FTPushButton* savePointButton;
    FTPushButtonIcon* deletePointButton;
    FTPushButton* useGpsButton;
    FTHelpWidget* help2;


    QLayout* createHorizontal();
    QLayout* createVertical();

    // List of points
    QList<map_cal_point> points;
    // Currently selected point
    int selected_point;
    // Map
    const Map* map;

    // The current widget state
    state currentState;

    // Whether we are movin with the mouse
    bool mouseMoving;
    // Mouse starting cohordinates (on mousePress)
    QPoint mouse_start;

    // This is the screen center (in SCREEN pixels)
    int center_x;
    int center_y;
    // The scale (MAP cohordinates : screen cohosrindates * scale)
    int scale;

    QPixmap center_cross;
};

#endif // MAPCALIBRATOR_H
